package com.example.work3.tools;

import com.auth0.jwt.interfaces.Claim;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Map;

@Component
public class LoginFilter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
       try {
           Map<String, Claim> claims = Jjwt.parseToken(token);
           claims.get("user").asMap().get("account");
           return true;
       }catch (Exception e){
           response.setCharacterEncoding("UTF-8");
           response.setContentType("text/html;charset=UTF-8");
           response.getWriter().write("401");
           return false;
       }
    }
}
